
indexModule.controller('adAll',function($scope,$http){
    //分页
    $scope.page = {
        page: 1,//当前页数
        limit: 10,// 每页显示多少条
        maxSize: 7 // 显示多少个分页按钮
    };

    //广告类型
    $scope.adTypes = [
        {name:"--类型--",value:""},
        {name:"360",value:"360"},
        {name:"搜狗",value:"sogou"},
        {name:"百度",value:"baidu"}
    ];
    $scope.ad_type = $scope.adTypes[0].value;

    //单价
    $scope.adPrice = [
        {name:"--单价--",value:""},
        {name:"高到低",value:-1},
        {name:"低到高",value:1}
    ];
    $scope.ad_price = $scope.adPrice[0].value;

    //请求所有广告list
    var postData = {page:$scope.page.page,size:$scope.page.limit};
    adlist(postData,$scope.page.page);
    function adlist(postData,page){
        postHttp($http,config.adShareList_httpUrl,postData,function(data){
            $scope.tableData = data.data.list;
            $scope.page.page = page;
            $scope.page.total = data.data.total;// list总数量
            $scope.page.numPages = Math.ceil(data.data.total/$scope.page.limit); // 总页数， 101/20 =6, 这个分页那个自动计算的值。
        });
    };

    //点击搜索
    var hasSearch = false;
    $scope.search = function(){
        var listData = {page:1,size:$scope.page.limit};
        if($scope.name) listData.name = $scope.name;
        if($("#search_start").val()) listData.start = $("#search_start").val();
        if($("#search_end").val()) listData.end = $("#search_start").val();
        if($scope.ad_type) listData.type = $scope.ad_type;
        if($scope.ad_price) listData.price = $scope.ad_price;
        adlist(listData,1);
        hasSearch = true;
    };

    //页码改变时
    $scope.pageChanged = function(){
        if(typeof(localStorage) === "object"){
            localStorage.setItem("limit",$scope.page.limit);
        };
        if(parseInt($scope.page.page) > parseInt($scope.page.numPages)){
            $scope.page.page = parseInt($scope.page.numPages);
        }else if(parseInt($scope.page.page) < 1){
            $scope.page.page = 1;
        }
        if(hasSearch){
            var postData = {page:1,size:$scope.page.limit};
            if($scope.name) listData.name = $scope.name;
            if($("#search_start").val()) listData.start = $("#search_start").val();
            if($("#search_end").val()) listData.end = $("#search_start").val();
            if($scope.ad_type) listData.type = $scope.ad_type;
            if($scope.ad_price) listData.price = $scope.ad_price;
        }else{
            var postData = {page:$scope.page.page,size:$scope.page.limit};
        }
        adlist(postData,$scope.page.page);
    };

});

